package class05;

import class02.ReverseList4;
import com.sun.xml.internal.bind.v2.model.core.ID;

import java.util.HashMap;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class TrieTree{
    public static class Node1{
        public int pass;
        public int end;
        public Node1[] nexts;

        public Node1(){
            pass = 0;
            end = 0;
            nexts = new Node1[26];
        }
    }

    public static class Trie1{
        private Node1 root;

        public Trie1(){
            root = new Node1();
        }

        public void insert(String word){
            if (word==null){
                return;
            }
            char[] str = word.toCharArray();
            Node1 node = root;
            node.pass++;
            int path = 0;
            for (int i=0;i<str.length;i++){
                path = str[i]-'a';
                if (node.nexts[path]==null)
                    node.nexts[path]=new Node1();
                node = node.nexts[path];
                node.pass++;
            }
            node.end++;
        }

        public int search(String word){
            if (word==null)
                return 0;
            char[] str = word.toCharArray();
            Node1 node = root;
            int path = 0;
            for (int i=0;i<str.length;i++){
                path=str[i]-'a';
                if (node.nexts[path]==null)
                    return 0;
                node = node.nexts[path];
            }
            return node.end;
        }

        public void delete(String word){
            if (search(word)!=0){
                char[] str = word.toCharArray();
                Node1 node = root;
                node.pass--;
                int path = 0;
                for (int i=0;i<str.length;i++){
                    path = str[i]-'a';
                    if (--node.nexts[path].pass==0)
                        node.nexts[path]=null;
                    node=node.nexts[path];
                }
                node.end--;
            }
        }

        public int prefixNumber(String pre){
            if (pre == null)
                return 0;
            char[] str = pre.toCharArray();
            Node1 node = root;
            int path = 0;
            for (int i=0;i<str.length;i++){
                path=str[i]-'a';
                if (node.nexts[path]==null)
                    return 0;
                node=node.nexts[path];
            }
            return node.pass;
        }
    }



    public static class Node2{
        public int pass;
        public int end;
        public HashMap<Integer,Node2> nexts;

        public Node2(){
            pass = 0;
            end = 0;
            nexts = new HashMap<>();
        }
    }

    public static class Trie2{
        private Node2 root;

        public Trie2(){
            root = new Node2();
        }

        public void insert(String word){
            if (word==null)
                return;
            char[] str = word.toCharArray();
            Node2 node = root;
            node.pass++;
            int path = 0;
            for (int i=0;i<str.length;i++){
                path = (int)str[i];
                if (!node.nexts.containsKey(path))
                    node.nexts.put(path,new Node2());
                node = node.nexts.get(path);
                node.pass++;
            }
            node.end++;
        }

        public int search(String word){
            if (word==null)
                return 0;
            char[] str = word.toCharArray();
            Node2 node = root;
            int path = 0;
            for (int i=0;i<str.length;i++){
                path=(int)str[i];
                if (!node.nexts.containsKey(path))
                    return 0;
                node=node.nexts.get(path);
            }
            return node.end;
        }

        public void delete(String word){
            if (search(word)!=0){
                char[] str = word.toCharArray();
                Node2 node = root;
                node.pass--;
                int path = 0;
                for (int i=0;i<str.length;i++){
                    path = (int)str[i];
                    if (--node.nexts.get(path).pass==0){
                        node.nexts.remove(path);
                        return;
                    }
                    node = node.nexts.get(path);
                }
                node.end--;
            }
        }

        public int prefixNumber(String pre){
            if (pre == null)
                return 0;
            char[] str = pre.toCharArray();
            Node2 node = root;
            int path = 0;
            for (int i=0;i<str.length;i++){
                path=(int) str[i];
                if (!node.nexts.containsKey(path)){
                    return 0;
                }
                node = node.nexts.get(path);
            }
            return node.pass;
        }
    }












}
